package top.ahzy.seobench.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.ahzy.seobench.mapper.UserMapper;
import top.ahzy.seobench.model.rsp.MessageOnLineRsp;
import top.ahzy.seobench.service.MessageService;
import top.ahzy.seobench.websocket.MessageSocket;

import javax.annotation.Resource;
import javax.websocket.Session;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class MessageServiceImpl implements MessageService {
    @Resource
    UserMapper userMapper;

    @Override
    public MessageOnLineRsp getOnLine() {
        List<String> userNameList = userMapper.getAllUserName();
        List<String> onLineList = new ArrayList<>();
        List<String> offLineList = new ArrayList<>();
        for (String userName : userNameList) {
            if(MessageSocket.sessionMap.containsKey(userName)){
                onLineList.add(userName);
            }else {
                offLineList.add(userName);
            }
        }

        MessageOnLineRsp rsp = new MessageOnLineRsp();
        rsp.setOnLineList(onLineList);
        rsp.setOffLineList(offLineList);
        return rsp;
    }
}
